package yuyicman.managers 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import yuyicman.debug.Debugger;
	import yuyicman.events.DataProviderEvent;
	import yuyicman.events.PageEvent;
	import yuyicman.utils.SWFAddress;
	import yuyicman.utils.SWFAddressEvent;
	/**
	 * ...
	 * @author eric.yu
	 */
	 
	 
	[Event(name = 'changed', type = 'yuyicman.events.PageEvent')]
	
	public class PageManager extends EventDispatcher
	{
		
		
		private static var _instance:PageManager;
		
		
		private var _pagesByName:Object;
		private var _isPageClosing:Boolean;
		
		private var _page:IPage;
		public function get page():IPage {
			return _page;
		}
		
		public function addPage( page:IPage ):IPage {
			
			if ( _pagesByName.hasOwnProperty( page.getPageName() ) ) {
				Debugger.log( "[PageManager addPage] Page '" + page.getPageName() + "' already exists. The old page instance will be replaced!", Debugger.ERROR );
			}
			
			_pagesByName[ page.getPageName() ] = page;
			return page;
			
		}
		
		public function pageTo( page:IPage ):void {
			
			if ( !_pagesByName.hasOwnProperty( page.getPageName() ) ) {
				addPage( page );
			}
			
			SWFAddress.setValue( page.getPageName() );
			
		}
		
		public function pageToByName( pageName:String ):void {
			
			if ( _pagesByName.hasOwnProperty(pageName) ) {
				
				SWFAddress.setValue( pageName );
				
			}
			else {
				Debugger.log( "[PageManager pageToByName]Page skip failed. page '" + pageName + "' not found!",Debugger.ERROR );
			}
			
		}
		
		
		public function PageManager( singleton:Singleton ) 
		{
			_pagesByName = { };
			_isPageClosing = false;
			SWFAddress.addEventListener( SWFAddressEvent.CHANGE,_swfAddressHandler );
		}
		
		private function _pageClosedHandler( e:PageEvent ):void {
			IPage(e.target).removeEventListener( PageEvent.CLOSE, _pageClosedHandler );
			_isPageClosing = false;
			_openCurrentPage();
		}
		
		private function _openCurrentPage():void {
			_page.open();
			dispatchEvent( new PageEvent( PageEvent.CHANGED ) );
		}
		
		private function _swfAddressHandler( e:SWFAddressEvent ):void {
			
			
			if ( _pagesByName.hasOwnProperty( e.value ) ) {
				
				var _temp:IPage = _page;
				
				_page = _pagesByName[ e.value ];
				
				if ( _temp && !_isPageClosing ) {
					_isPageClosing = true;
					_temp.addEventListener( PageEvent.CLOSE, _pageClosedHandler );
					_temp.close();
				}
				
				if ( _temp==null ) {
					_openCurrentPage();
				}
				
				
				//_page.open();
				
				//dispatchEvent( new DataProviderEvent( CHANGED, _page ) );
				
			}
			else {
				Debugger.log( "[PageManager _swfAddressHandler]Page '" + e.value + "' not defined!",Debugger.WARNING );
			}
			
		}
		
		public static function addPage( page:IPage ):IPage {
			return instance.addPage( page );
		}
		
		public static function pageToByName( pageName:String ):void {
			instance.pageToByName( pageName );
		}
		
		public static function pageTo( page:IPage ):void {
			instance.pageTo( page );
		}
		
		public static function get instance():PageManager {
			
			if ( !_instance ) {
				_instance = new PageManager( new Singleton );
			}
			return _instance;
			
		}
		
	}

}

internal class Singleton{}